package servlet;

import Dbase.dataBase;
import model.userData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(name = "servlet_register",urlPatterns = "/Pages/register")
public class servlet_register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String pwd = req.getParameter("password");
        String repwd = req.getParameter("repassword");
        PrintWriter out = resp.getWriter();
        if(pwd.equals(repwd)){
            try {
                dataBase db = new dataBase();
                if(db.getId(name)==-1) {
                    db.insert(name, pwd);
                    out.println("注册成功" + "<br>" + "你的uid为" + db.getId(name));
                }
                else{
                    out.println("账户已存在");
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req,resp);
    }
}
